﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BudgetManager.Domain;
using Microsoft.Phone.Shell;

namespace BudgetManager
{
    public partial class EditValue : PhoneApplicationPage
    {
        Category _category;
        AbstractValue _abstractValue;
        public EditValue()
        {
            InitializeComponent();
             if (isEditMode())
           {
               _abstractValue = (AbstractValue)PhoneApplicationService.Current.State["abstractvalue"];
               _category = _abstractValue.Category;
               txtDescription.Text = _abstractValue.Description;
               txtCategory.Text = _category.Name;
               txtMoney.Text = _abstractValue.Money.ToString();
   
               datePicker.Value = _abstractValue.FullDate;
           }

            Object category;
            if (PhoneApplicationService.Current.State.TryGetValue("Category", out category) && category!=null)
            {
                _category = (Category)category; // save state
                txtCategory.Text = _category.Name; //change text box
                PhoneApplicationService.Current.State["Category"] = null; // Clean memory
            }
        
        }
       

        private void onCancel(object sender, EventArgs e)
        {


          //  NavigationService.Navigate(new Uri(string.Format("/ViewValues.xaml?"), UriKind.Relative));
            NavigationService.GoBack();
        }

       
        private void onTapMoney(object sender, System.Windows.Input.GestureEventArgs e)
        {
            txtMoney.Text = String.Empty;
        }
     
        private void txtMoney_TextChanged(object sender, TextChangedEventArgs e)
        {
             
            
        }

        private bool isEditMode()
        {
            object abstractValue;
            return PhoneApplicationService.Current.State.TryGetValue("abstractvalue", out abstractValue) && abstractValue != null; 
        }
 

        private  void onTapCategories(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
          // NavigationService.Navigate(new Uri(string.Format("/ManagementCategory.xaml?"), UriKind.Relative));
        
        }

        private void onTapDescription(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //txtDescription.Text = String.Empty;
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            if (_abstractValue is Outcome)
            {
                AccountManager.SelectedAccount.editExpense(_abstractValue.Id, _category, txtDescription.Text, Int32.Parse(txtMoney.Text));
            }
            else
            {
                AccountManager.SelectedAccount.editIncome(_abstractValue.Id, _category, txtDescription.Text, Int32.Parse(txtMoney.Text));
            }
          //  NavigationService.Navigate(new Uri(string.Format("/ViewValues.xaml?"), UriKind.Relative));
            
            NavigationService.GoBack();
        }
        private void onTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //NavigationService.Navigate(new Uri(string.Format("/CategoryExpense.xaml?"), UriKind.Relative));
        }
    }
}
